package com.zjzywl.smoke.controller;

import java.lang.reflect.Type;
import javax.websocket.server.PathParam;

import com.zjzywl.smoke.entity.Building;
import com.zjzywl.smoke.util.RestPageImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import com.zjzywl.smoke.authenticate.AuthUser;
import com.zjzywl.smoke.config.AppConfig;
import com.zjzywl.smoke.entity.SysUser;
import com.zjzywl.smoke.util.DisposeAlarm;
import com.zjzywl.smoke.util.WebResult;
import org.springframework.web.util.UriComponentsBuilder;

@Controller
@RequestMapping(value = "/alarm")
public class AlarmController extends BaseController {
    @Autowired
    private AppConfig appConfig;

    // 报警历史记录查询 新接口--返回数据精简
    @GetMapping(value = "/data/history/simple/list")
    @ResponseBody
    public WebResult getAlarmSimple(@PathParam("beginTime") String beginTime, @PathParam("endTime") String endTime,
            @PathParam("alarmTypeCode") String alarmTypeCode, @PathParam("deviceTypeId") String deviceTypeId,
            @PathParam("address") String address, @PathParam("alarmReasons") String alarmReasons,@PathParam("devCode") String devCode) {
        String userId = this.getUserId();
        ResponseEntity<Object> alarms = appConfig.restTemplate()
                .exchange(appConfig.getGetDataIp() + "/alarm/data/history/simple/list?beginTime=" + beginTime
                        + "&endTime=" + endTime + "&alarmTypeCode=" + alarmTypeCode + "&deviceTypeId=" + deviceTypeId
                        + "&address=" + address + "&userId=" + userId + "&alarmReasons=" + alarmReasons+"&devCode="+devCode, HttpMethod.GET,
                        null, Object.class);
        return WebResult.OK(alarms);
    }

    // 按照时间段、组织统计每天报警数量
    @RequestMapping(value = "/data/org/device/count")
    @ResponseBody
    public WebResult getOrgAlarmCountByTimeEx(@PathParam("beginTime") String beginTime,
            @PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
            @PathParam("deviceTypeId") String deviceTypeId) {
        String userId = this.getUserId();
        ResponseEntity<Object> alarms = appConfig.restTemplate().exchange(
                appConfig.getGetDataIp() + "/alarm/data/org/device/count?beginTime=" + beginTime + "&endTime=" + endTime
                        + "&alarmTypeCode=" + alarmTypeCode + "&deviceTypeId=" + deviceTypeId + "&userId=" + userId,
                HttpMethod.GET, null, Object.class);
        return WebResult.OK(alarms);

    }

    // 获取未处理的列表
    @RequestMapping(value = "/notdispose")
    @ResponseBody
    public WebResult getNotDisposeBy() {
        String userId = this.getUserId();
        ResponseEntity<Object> alarmTypes = appConfig.restTemplate()
                .getForEntity(appConfig.getGetDataIp() + "alarm/data/notdispose/smoke/" + userId, Object.class);
        return WebResult.OK(alarmTypes);
    }

    // 处理报警
    @RequestMapping(value = "/disposealarm")
    @ResponseBody
    public WebResult disposeAlarm(DisposeAlarm disposeAlarm) {
        AuthUser loginUser = this.getLoginUser();
        SysUser people = loginUser.getPeople();
        String name = people.getName();
        System.out.println(appConfig.getGetDataIp() + "/alarm/data/disposealarm" + "/" + disposeAlarm.getDevCode() + "/"
                + disposeAlarm.getAlarmId() + "/" + disposeAlarm.getDisposeStatus() + "/"
                + disposeAlarm.getRemark() + "/" + disposeAlarm.getAlarmReasons() + "/" + name);
        ResponseEntity<Object> exchange = appConfig.restTemplate().postForEntity(
                appConfig.getGetDataIp() + "/alarm/data/disposealarm" + "/" + name,
                disposeAlarm, Object.class);
        return WebResult.OK(exchange.getBody());
    }

    // 统计异常设备上报信息
    @RequestMapping(value = "/count/bydevicetype")
    @ResponseBody
    public WebResult getCountByStatusAndDeviceTypeAndUser(@PathParam("status") String status,
            @PathParam("deviceTypeId") String deviceTypeId, @PathParam("startTime") String startTime,
            @PathParam("endTime") String endTime) {
        String userId = this.getUserId();
        ResponseEntity<Object> devices = appConfig.restTemplate()
                .exchange(
                        appConfig.getGetDataIp() + "/deviceStatus/count/bydevicetype/" + status + "/" + deviceTypeId
                                + "/" + userId + "" + "/" + startTime + "/" + endTime,
                        HttpMethod.GET, null, Object.class);
        try {
            return WebResult.OK(devices);
        } catch (Exception e) {
            return WebResult.FAIL(e.getMessage());
        }
    }

    // 统计报警信息
    @RequestMapping(value = "/history/page")
    @ResponseBody
    public WebResult getHistory(@RequestParam("beginTime") String beginTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            @RequestParam(value = "alarmTypeCode", required = false) String alarmTypeCode,
            @RequestParam(value = "deviceTypeId", required = false) String deviceTypeId,
            @RequestParam(value = "address", required = false) String address,
            @RequestParam(value = "alarmReasons", required = false) String alarmReasons, Pageable pageable) {

        try {
            String userId = this.getUserId();
            if (userId.isEmpty()) {
                userId = "1";
            }
            UriComponentsBuilder builder = UriComponentsBuilder
                    .fromHttpUrl(appConfig.getGetDataIp() + "/alarm/history/page");
            builder.queryParam("userId", userId).queryParam("beginTime", beginTime);
            if (endTime != null && !endTime.isEmpty()) {
                builder.queryParam("endTime", endTime);
            }
            if (alarmTypeCode != null && !alarmTypeCode.isEmpty()) {
                builder.queryParam("alarmTypeCode", alarmTypeCode);
            }
            if (deviceTypeId != null && !deviceTypeId.isEmpty()) {
                builder.queryParam("deviceTypeId", deviceTypeId);
            }
            if (address != null && !address.isEmpty()) {
                builder.queryParam("address", address);
            }
            if (alarmReasons != null && !alarmReasons.isEmpty()) {
                builder.queryParam("alarmReasons", alarmReasons);
            }
            if (pageable != null) {
                builder.queryParam("size", String.valueOf(pageable.getPageSize()));
                builder.queryParam("offset", String.valueOf(pageable.getOffset()));
            }
            ParameterizedTypeReference<RestPageImpl<Object>> typeReference = new ParameterizedTypeReference<RestPageImpl<Object>>() {
                @Override
                public Type getType() {
                    return super.getType();
                }
            };
            ResponseEntity<RestPageImpl<Object>> devices = appConfig.restTemplate().exchange(builder.toUriString(),
                    HttpMethod.GET, null, typeReference);
            return WebResult.OK(devices);
        } catch (Exception e) {
            return WebResult.FAIL(e.getMessage());
        }
    }

    // 按照时间段统计设备的报警数量
    @RequestMapping(value = "/data/device/count")
    @ResponseBody
    public WebResult getDeviceAlarmCountEx(@PathParam("beginTime") String beginTime,
            @PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
            @PathParam("deviceTypeId") String deviceTypeId) {
        String userId = this.getUserId();
        ResponseEntity<Object> exchange = appConfig.restTemplate()
                .exchange(appConfig.getGetDataIp() + "/alarm/data/device/count" + "?beginTime=" + beginTime
                        + "&endTime=" + endTime + "&alarmTypeCode=" + alarmTypeCode + "&deviceTypeId=" + deviceTypeId
                        + "&userId=" + userId, HttpMethod.POST, null, Object.class);
        return WebResult.OK(exchange);

    }
    //统计响应速度
    @RequestMapping(value = "/data/respond/count")
    @ResponseBody
    public WebResult findByTimeDifference(@PathParam("beginTime") String beginTime,
            @PathParam("endTime") String endTime, @PathParam("alarmTypeCode") String alarmTypeCode,
            @PathParam("deviceTypeId") String deviceTypeId) {
        String userId = this.getUserId();
        ResponseEntity<Object> exchange = appConfig.restTemplate()
                .exchange(appConfig.getGetDataIp() + "/alarm/data/respond/count" + "?beginTime=" + beginTime
                        + "&endTime=" + endTime + "&alarmTypeCode=" + alarmTypeCode + "&deviceTypeId=" + deviceTypeId
                        + "&userId=" + userId, HttpMethod.POST, null, Object.class);
        return WebResult.OK(exchange);

    }

    // 按照时间段、组织统计报警来源和数量
    @RequestMapping(value = "/data/org/device/fixcount")
    @ResponseBody
    public WebResult getDeviceCountByFixedPlace( @RequestParam("beginTime")String beginTime,
            @RequestParam("endTime")String endTime, @RequestParam("alarmTypeCode") Integer alarmTypeCode,
            @RequestParam("deviceTypeId") int deviceTypeId) {
        String userId = this.getUserId();
        ResponseEntity<Object> exchange = appConfig.restTemplate()
                .exchange(appConfig.getGetDataIp() + "/alarm/data/org/device/fixcount?beginTime=" + beginTime
                        + "&endTime=" + endTime + "&alarmTypeCode=" + alarmTypeCode + "&deviceTypeId=" + deviceTypeId
                        + "&userId=" + userId, HttpMethod.POST, null, Object.class);
        return WebResult.OK(exchange);
    }
    // 按照时间段、组织统计报警原因和数量
    @RequestMapping(value = "/data/org/device/alarmReason")
    @ResponseBody
    public WebResult getOrgAlarmCountByType(@RequestParam("beginTime")String beginTime,
            @RequestParam("endTime")String endTime, @RequestParam("alarmTypeCode") Integer alarmTypeCode,
            @RequestParam("deviceTypeId") int deviceTypeId) {
        String userId = this.getUserId();
        ResponseEntity<Object> exchange = appConfig.restTemplate()
                .exchange(appConfig.getGetDataIp() + "/alarm/data/org/device/alarmReason?beginTime=" + beginTime
                        + "&endTime=" + endTime + "&alarmTypeCode=" + alarmTypeCode + "&deviceTypeId=" + deviceTypeId
                        + "&userId=" + userId, HttpMethod.POST, null, Object.class);
        return WebResult.OK(exchange);
    }


}
